import numpy as np;
import matplotlib.pyplot as plt;
import sympy as sp;

x = sp.Symbol('x')

# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False

# 分析数列极限
n = sp.symbols('n', integer=True)
a_n = n/(n+1)

# 计算数列极限
sequence_limit = sp.limit(a_n, n, sp.oo)
print(f"数列a_n = n/(n+1)的极限: {sequence_limit}")

# 可视化数列收敛过程
n_vals = np.arange(1, 101)
a_n_vals = n_vals / (n_vals + 1)

plt.figure(figsize=(12, 5))

plt.subplot(1, 2, 1)
plt.plot(n_vals, a_n_vals, 'bo-', markersize=3, linewidth=1)
plt.axhline(y=1, color='r', linestyle='--', alpha=0.7, label='极限值y=1')
plt.xlabel('n')
plt.ylabel('a_n')
plt.title('数列a_n = n/(n+1)的收敛过程')
plt.legend()
plt.grid(True, alpha=0.3)

# 展示收敛速度（误差随n增大而减小）
errors = np.abs(a_n_vals - 1)
plt.subplot(1, 2, 2)
plt.semilogy(n_vals, errors, 'ro-', markersize=3, linewidth=1)
plt.xlabel('n')
plt.ylabel('|a_n - 1| (对数尺度)')
plt.title('数列收敛速度（误差衰减）')
plt.grid(True, alpha=0.3)

plt.tight_layout()
plt.show()